﻿namespace SCCM_Client_Injector_2014
{
    using Microsoft.ConfigurationManagement.ManagementProvider;
    using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
    using System;
    using System.Windows.Forms;

    internal class WMIConnect
    {
        public WqlConnectionManager Connect(string serverName, string userName, string userPassword)
        {
            try
            {
                SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
                WqlConnectionManager manager = new WqlConnectionManager(namedValues);
                manager.Connect(serverName);
                Application.DoEvents();
                LogFile.LogEvent("", 0, "", "", "DBG", "WMI Connection | Success, server: " + serverName);
                return manager;
            }
            catch (SmsException exception)
            {
                MessageBox.Show("Failed to Connect. Error: " + exception.Message);
                LogFile.LogEvent("", 0, "", "", "ERR", "WMI Connection | Failed to connect: " + exception.Message);
                return null;
            }
            catch (UnauthorizedAccessException exception2)
            {
                MessageBox.Show("Failed to authenticate. Error:" + exception2.Message);
                LogFile.LogEvent("", 0, "", "", "ERR", "WMI Connection | Failed to authenticate: " + exception2.Message);
                return null;
            }
        }
    }
}

